package com.ddx.service.impl;

import com.ddx.common.redis.RedisKey;
import com.ddx.common.redis.RedisService;
import com.ddx.common.util.DataUtil;
import com.ddx.common.util.SmsUtils;
import com.ddx.enums.SmsCodeEnum;
import com.ddx.service.SmsCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: liyuxiang
 * @create: 2018-11-04
 */
@Service
public class SmsCodeServiceImpl implements SmsCodeService {

    private static Logger logger = LoggerFactory.getLogger(SmsCodeServiceImpl.class);

    @Autowired
    RedisService redisService;

    private static final Long ten_minute = 10 * 60L;

    @Override
    public void sendSmsCode(String mobile, String type) {
        logger.info("发送验证码入参：mobile={},type={}",mobile,type);
        SmsCodeEnum smsCodeEnum = SmsCodeEnum.getByValue(type);
        String value = DataUtil.getRandomStr(6);
        redisService.set(RedisKey.SMS_CODE + mobile + "_" + type, value, ten_minute);
        SmsUtils.sendSms(mobile, value, smsCodeEnum.getCode());
    }

}
